import { RoomSafeState } from '@/room/state/types'
import TransportTaskManager from '@/room/task/base/transport'
import { OneToOneTransportData } from '@/room/task/base/transport/types'
import PCTaskManager from '@/room/task/pc'

/**
 * PowerSpawn 控制器
 */
export default class PowerSpawnController {
    /**
     * 各房间烧抛瓦休眠时间
     */
    public static sleepTime: { [roomName: string]: number } = {}

    /**
     * 搬运抛瓦任务名字
     */
    public static readonly TRANSPORT_POWER_TASK_NAME = '搬运烧抛瓦的资源'

    /**
     * 运行
     */
    public static run(room: MyRoom) {
        this.processPower(room)
    }

    /**
     * 烧抛瓦
     */
    public static processPower(room: MyRoom) {
        const sleepTime = PowerSpawnController.sleepTime[room.name] || 0

        if (sleepTime) {
            PowerSpawnController.sleepTime[room.name] = sleepTime - 1
            return
        }

        if (Memory.meta.stopProcessPower) return

        const powerSpawn = room.powerSpawn

        if (!powerSpawn) {
            PowerSpawnController.sleepTime[room.name] = 3000
            return
        }

        if (
            (powerSpawn.store['power'] < 20 || powerSpawn.store['energy'] < 1000) &&
            !TransportTaskManager.searchTransportTaskByName(room, PowerSpawnController.TRANSPORT_POWER_TASK_NAME).length
        ) {
            if (Game.cpu.bucket < 100) {
                PowerSpawnController.sleepTime[room.name] = 300
                return
            }

            if (Game.market.credits < 100 * 1e6) {
                PowerSpawnController.sleepTime[room.name] = 3000
                return
            }

            const requirePower = POWER_SPAWN_POWER_CAPACITY - powerSpawn.store['power']
            const requireEnergy = POWER_SPAWN_ENERGY_CAPACITY - powerSpawn.store['energy']
            const minEnergy = room.memory.state.safe?.state === RoomSafeState.normal ? 30000 : 40000
            let target: StructureStorage | StructureTerminal | null = null
            if (
                room.storage &&
                room.storage.store['power'] >= requirePower &&
                room.storage.store['energy'] >= minEnergy
            ) {
                target = room.storage
            } else if (
                room.terminal &&
                room.terminal.store['power'] >= requirePower &&
                room.terminal.store['energy'] >= minEnergy
            ) {
                target = room.terminal
            }
            if (!target) {
                PowerSpawnController.sleepTime[room.name] = 300
                return
            }

            const data: OneToOneTransportData = {
                sourceId: target.id,
                targetId: powerSpawn.id,
                resInfo: {
                    [RESOURCE_POWER]: {
                        require: requirePower,
                        has: 0,
                    },
                    [RESOURCE_ENERGY]: {
                        require: requireEnergy,
                        has: 0,
                    },
                },
            }

            TransportTaskManager.addOneToOneTransportTask(room, PowerSpawnController.TRANSPORT_POWER_TASK_NAME, data)
        }

        if (powerSpawn.store['energy'] > 50 && powerSpawn.store['power'] > 1) {
            const result = powerSpawn.processPower()
            if (result === OK) {
                PCTaskManager.autoAddTask(room, PWR_OPERATE_POWER, powerSpawn, 111, 10)
            }

            if (result === ERR_NOT_OWNER || result === ERR_RCL_NOT_ENOUGH) {
                PowerSpawnController.sleepTime[room.name] = 3000
            }
        } else {
            PowerSpawnController.sleepTime[room.name] = 7
        }
    }
}
